/* 
 * GWT-Ext Widget Library 
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated 
 * by the @authors tag. See the copyright.txt in the distribution for a 
 * full listing of individual contributors. 
 * 
 * This is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version. 
 * 
 * This software is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this software; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 */

package edu.rpi.tw.pml.browsing.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.rpi.tw.pml.browsing.client.util.JSONParser;

/**
 * Entry point class defining <code>onModuleLoad()</code>.
 */
public class PMLFacetedSearch implements EntryPoint {

	// Remote service proxy to talk to the server.
	private final FacetSearchServiceAsync facetSearchService = GWT
			.create(FacetSearchService.class);

	// An instance table.
	private TableDataWidget instanceTable = new TableDataWidget();

	// A set of facets.
	private String[] facets;
	private FacetSetWidget facetSet;

	// A concept tree.
	private ConceptTreeWidget conceptTree = new ConceptTreeWidget(instanceTable,
			facetSet);

	/**
	 * Entry point method.
	 */
	public void onModuleLoad() {

		Panel panel = new Panel();
		panel.setBorder(false);
		panel.setPaddings(15);
		panel.setLayout(new FitLayout());

		Panel borderPanel = new Panel();
		borderPanel.setLayout(new BorderLayout());

		// north panel.
		BoxComponent northPanel = new BoxComponent();
		northPanel.setEl(new HTML(
				"<p>This section is generally for menus, toolbars"
						+ " and/or advertisements</p>").getElement());
		northPanel.setHeight(32);
		borderPanel.add(northPanel, new BorderLayoutData(RegionPosition.NORTH));

		
		// east panel.
		final Panel eastPanel = new Panel();
		eastPanel.setTitle("Facet List");
		eastPanel.setCollapsible(true);
		eastPanel.setWidth(408);
		eastPanel.setLayout(new FitLayout());

		BorderLayoutData eastData = new BorderLayoutData(RegionPosition.EAST);
		eastData.setSplit(true);
		eastData.setMinSize(300);
		eastData.setMaxSize(408);
		eastData.setMargins(new Margins(0, 0, 5, 0));
		
		// load all facets via remote SPARQL endpoint.
		facetSearchService.getAllFacets(new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out
						.println("Error while loading facet names.");
			}

			public void onSuccess(String result) {
				facets = JSONParser.string2FacetArray(result);
				facetSet = new FacetSetWidget(facets);
				eastPanel.add(facetSet.getPanel());
				eastPanel.doLayout();
			}
		});
		borderPanel.add(eastPanel, eastData);

		// west panel.
		Panel westPanel = new Panel();
		westPanel.setTitle("Resource Explorer");
		westPanel.setCollapsible(true);
		westPanel.setWidth(300);
		westPanel.setLayout(new FitLayout());

		BorderLayoutData westData = new BorderLayoutData(RegionPosition.WEST);
		westData.setSplit(true);
		westData.setMinSize(300);
		westData.setMaxSize(300);
		westData.setMargins(new Margins(0, 5, 0, 0));

		westPanel.add(conceptTree.getPanel());
		borderPanel.add(westPanel, westData);

		// center panel.
		TabPanel centerPanel = new TabPanel();
		centerPanel.setDeferredRender(false);
		centerPanel.setActiveTab(0);

		Panel instanceListPanel = new Panel();
		instanceListPanel.setTitle("Instance List");
		instanceListPanel.setLayout(new FitLayout());

		instanceListPanel.add(instanceTable.getPanel());
		centerPanel.add(instanceListPanel);

		Panel instancePanel = new Panel();
		instancePanel.setTitle("Instance");
		instancePanel.setClosable(true);
		instancePanel.setLayout(new FitLayout());

		centerPanel.add(instancePanel);

		borderPanel.add(centerPanel,
				new BorderLayoutData(RegionPosition.CENTER));

		panel.add(borderPanel);
		
		Viewport view = new Viewport(panel);
		view.doLayout();
	}

	private void loadFacets() {

	}
}